package com.prestamomo.vil.ppa.pmvilpage;

import android.app.AlertDialog;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilQuestion;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerConfig;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilSerConfigFiter;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import java.io.Serializable;
import java.util.List;

public class PmvilAdviseVy extends PmvilAtyRoot {

    private ImageView advisePmvilIvBack;
    private LinearLayout advisePmvilLlhisty;
    private RelativeLayout advisePmvilRltype;
    private TextView advisePmvilTvtype;
    private EditText advisePmvilEtconte;
    private TextView advisePmvilTvcomit;
    private List<PmvilSerConfig> pmvilListQuestions;
    private PmvilSerConfig.ExtInfoArrPmvil cofigExtInfoArrPmvil;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.advise_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        advisePmvilIvBack = findViewById(R.id.advise_pmvil_iv_back);
        advisePmvilLlhisty = findViewById(R.id.advise_pmvil_ll_histy);
        advisePmvilRltype = findViewById(R.id.advise_pmvil_rl_type);
        advisePmvilTvtype = findViewById(R.id.advise_pmvil_tv_type);
        advisePmvilEtconte = findViewById(R.id.advise_pmvil_et_conte);
        advisePmvilTvcomit = findViewById(R.id.advise_pmvil_tv_comit);
    }

    @Override
    protected void clickePmvil() {
        advisePmvilIvBack.setOnClickListener(v -> finish());
        advisePmvilLlhisty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getQuestionPmvilList();
            }
        });
        advisePmvilRltype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pmvilListQuestions != null) {
                    showPmvilQuestPop(advisePmvilRltype);
                }
            }
        });
        advisePmvilEtconte.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && !TextUtils.isEmpty(advisePmvilTvtype.getText().toString())) {
                    advisePmvilTvcomit.setEnabled(true);
                    advisePmvilTvcomit.setBackgroundResource(R.drawable.sp_pm_tvbg_hs);
                } else {
                    advisePmvilTvcomit.setEnabled(false);
                    advisePmvilTvcomit.setBackgroundResource(R.drawable.sp_pm_bg_hs_20);
                }
            }
        });
        advisePmvilTvcomit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postAdvisePmvilQuest(cofigExtInfoArrPmvil.getPmvilcode(), advisePmvilEtconte.getText().toString());
            }
        });
    }

    private void showPmvilQuestPop(RelativeLayout advisePmvilRltype) {
        View pmvilQuesPopview = LayoutInflater.from(this).inflate(R.layout.question_pmvil_pop_ly, null, false);
        final PopupWindow popWindow = new PopupWindow(pmvilQuesPopview, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popWindow.setTouchable(true);
        popWindow.showAsDropDown(advisePmvilRltype);
        ListView pmvilQuestList = pmvilQuesPopview.findViewById(R.id.question_pmvil_list);
        TextView pmvilQuestTvno = pmvilQuesPopview.findViewById(R.id.question_pmvil_tv_no);
        pmvilQuestTvno.setOnClickListener(v -> popWindow.dismiss());
        PmvilSerConfigFiter pmvilSerConfigFiter = new PmvilSerConfigFiter(pmvilAy, pmvilListQuestions.get(0).getPmvilextInfoArr());
        pmvilQuestList.setAdapter(pmvilSerConfigFiter);
        pmvilQuestList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popWindow.dismiss();
                advisePmvilTvtype.setText(pmvilListQuestions.get(0).getPmvilextInfoArr().get(position).getPmvilname());
                cofigExtInfoArrPmvil = pmvilListQuestions.get(0).getPmvilextInfoArr().get(position);
                if (!TextUtils.isEmpty(advisePmvilEtconte.getText().toString())) {
                    advisePmvilTvcomit.setEnabled(true);
                    advisePmvilTvcomit.setBackgroundResource(R.drawable.sp_pm_tvbg_hs);
                } else {
                    advisePmvilTvcomit.setEnabled(false);
                    advisePmvilTvcomit.setBackgroundResource(R.drawable.sp_pm_bg_hs_20);
                }
            }
        });
    }

    @Override
    protected void loadPmvilData() {
        showPmvilLoading();
        PmvilHttpRequest.getConfigser(this, "question_type", new PmvilCallBack<List<PmvilSerConfig>>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, List<PmvilSerConfig> pmvilContent) {
                hindPmvilLoading();
                pmvilListQuestions = pmvilContent;
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, List<PmvilSerConfig> pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, List<PmvilSerConfig> pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }


    private void postAdvisePmvilQuest(String pmvilCodeQuest, String pmvilContStr) {
        showPmvilLoading();
        PmvilHttpRequest.postPmvilQuest(this, pmvilCodeQuest, pmvilContStr, new PmvilCallBack<Boolean>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {
                hindPmvilLoading();
                finish();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }


    private void getQuestionPmvilList() {
        showPmvilLoading();
        PmvilHttpRequest.getUserPmvilQuest(this, new PmvilCallBack<List<PmvilQuestion>>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, List<PmvilQuestion> pmvilContent) {
                hindPmvilLoading();
                if (pmvilContent.size() != 0) {
                    Intent pmvilQuestIntent = new Intent(pmvilAy, PmvilQuestionListVy.class);
                    pmvilQuestIntent.putExtra("quest_list_pmvil", (Serializable) pmvilContent);
                    startActivity(pmvilQuestIntent);
                } else {
                    showPmvilQuestDlg();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, List<PmvilQuestion> pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, List<PmvilQuestion> pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }


    public void showPmvilQuestDlg() {
        AlertDialog.Builder permisPmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilPermisView = this.getLayoutInflater().inflate(R.layout.question_pmvil_dlg, null);
        permisPmvilBlder.setView(pmvilPermisView);
        permisPmvilBlder.setCancelable(false);
        AlertDialog pmvilPermisDlg = permisPmvilBlder.create();
        pmvilPermisDlg.show();

        ImageView pmvileImClose = pmvilPermisView.findViewById(R.id.quest_pmvildlg_close);
        TextView pmvilTvIno = pmvilPermisView.findViewById(R.id.quest_pmvildlg_tv_ok);
        pmvileImClose.setOnClickListener(v -> pmvilPermisDlg.dismiss());
        pmvilTvIno.setOnClickListener(v -> pmvilPermisDlg.dismiss());
    }
}
